package cn.schoolwow.workflow.module.task.action.flow.complete.step.businessType;

import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.common.domain.WorkFlowTaskType;
import cn.schoolwow.workflow.module.common.flow.AddWorkFlowHistoryFlow;
import cn.schoolwow.workflow.module.task.action.flow.common.SetWorkFlowTaskListCommonFlow;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import java.util.List;

public class HandleNormalBusinessNodeFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        updateWorkFlowTaskStatus(flowContext);
        insertWorkFlowTask(flowContext);
        addWorkFlowHistoryFlow(flowContext);
    }

    @Override
    public String name() {
        return "处理正常业务节点";
    }

    private void updateWorkFlowTaskStatus(FlowContext flowContext){
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);
        CompleteTaskRequest completeTaskRequest = flowContext.getInstanceData(CompleteTaskRequest.class);

        if(null!=completeTaskRequest){
            transaction.addRecordString("更新工作流任务为完成状态");
            transaction.query(WorkFlowTask.class)
                    .addQuery("id", completeTaskRequest.taskId)
                    .addUpdate("complete", true)
                    .execute()
                    .update();
        }
    }

    private void insertWorkFlowTask(FlowContext flowContext){
        WorkFlowNode nextNode = flowContext.checkData("nextNode", WorkFlowNode.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.addRecordString("处理正常审批业务节点-插入工作流任务记录");
        flowContext.startFlow(new SetWorkFlowTaskListCommonFlow())
                .putCurrentCompositeFlowInstanceData(nextNode)
                .execute();
        List<WorkFlowTask> workFlowTaskList = flowContext.getData("workFlowTaskList", List.class);
        if(workFlowTaskList.isEmpty()){
            flowContext.brokenCurrentFlow("");
        }
        for(WorkFlowTask workFlowTask:workFlowTaskList){
            workFlowTask.setType(WorkFlowTaskType.AuditTask.value);
        }
        transaction.insert(workFlowTaskList);
        for(WorkFlowTask workFlowTask:workFlowTaskList){
            Validate.isTrue(workFlowTask.getId()>0, "获取工作流任务自增id失败!");
        }
    }

    private void addWorkFlowHistoryFlow(FlowContext flowContext){
        WorkFlowNode nextNode = flowContext.checkData("nextNode", WorkFlowNode.class);
        List<WorkFlowTask> workFlowTaskList = flowContext.checkData("workFlowTaskList", List.class);

        List<String> assigners = QueryInstanceList.newQuery(workFlowTaskList)
                .execute()
                .getSingleFieldValueList("assigner");

        flowContext.startFlow(new AddWorkFlowHistoryFlow())
                .putCurrentCompositeFlowData("message", "添加审批任务["+nextNode.getName()+"]")
                .putCurrentCompositeFlowData("assigner", StringUtils.join(assigners, ","))
                .execute();
    }
}
